package com.chijun.mform.create.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.chijun.mform.R;
import com.chijun.mform.create.dialog.ISelectFormIcon;
import com.chijun.mform.create.mode.FormIcon;
import com.example.ccj.u.ImageUtils;

import java.util.List;


/**
 * 加载表单图标Adapter
 * 点击字段后返回字段类型id
 * 2024年11月10日23:38:06
 */
public class SelectFormIconAdapter extends RecyclerView.Adapter<SelectFormIconAdapter.ViewHolder>{

    List<FormIcon> iconList;
    ISelectFormIcon iSelectFormIcon;

    /**
     * 字段类型数组
     *
     * @param iconList 字段列表
     * @param iSelectFormIcon 通过此回调返回选择的字段编号
     */
    public SelectFormIconAdapter(List<FormIcon> iconList, @NonNull ISelectFormIcon iSelectFormIcon){
        this.iconList = iconList;
        this.iSelectFormIcon = iSelectFormIcon;
    }

    @NonNull
    @Override
    public SelectFormIconAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectFormIconAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapater_select_form_icon, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectFormIconAdapter.ViewHolder holder, int position) {
        FormIcon image = iconList.get(position);
        ImageUtils.loadImage(holder.img.getContext(),image.url,holder.img);
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (iSelectFormIcon != null) {
                    iSelectFormIcon.complete(image);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return iconList == null ? 0 : iconList.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
        }
    }
}
